﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeChange : MonoBehaviour
{
	// Start is called before the first frame update
	[Header("DayLightParameters")]
	public GameObject LightSourceDay;
	[Tooltip("Define intensity for the Warm-Daylight in Daytime.")]
	public float DayLightIntensityInDay;
	[Tooltip("Define intensity for the Warm-Daylight at Night.")]
	public float DayLightIntensityAtNight;
	[Tooltip("夜光旋转控制窗口")]
	[Header("NightLightParameters")]
	public GameObject LightSourceNight;
	[Tooltip("Define intensity for the Cold-Nightlight in Daytime.")]
	public float NightLightIntensityAtNight;
	[Tooltip("Define intensity for the Cold-Nightlight at Night.")]
	public float NightLightIntensityInDay;
	[Header("TimeChange")]
	[Tooltip("TimeScale in daytime.")]
	public float TimeChangeSpeedInDay;
	[Tooltip("TimeScale at night")]
	public float TimeChangeSpeedAtNight;
	[Header("Day-Night swap")]
	[Tooltip("Decide when the day comes")]
	public float DayComeAngle = 5;
	[Tooltip("Decide when the night comes")]
	public float NightComeAngle = 355;
	public float DayNightSwapSmooth = 2.0f;
	#region BasicParam
	TimeMode _current;
	private float TimeChangeSpeed = 1.0f;

	TimeMode Day;
	TimeMode Night;
	#endregion
	void Start()
	{
		Day = new TimeMode(TimeChangeSpeedInDay, DayLightIntensityInDay, NightLightIntensityInDay);
		Night = new TimeMode(TimeChangeSpeedAtNight, DayLightIntensityAtNight, NightLightIntensityAtNight);
		ChangeMode(Day);
	}
	struct TimeMode
	{
		public float TimeSpeed;
		public float DayLightIntensity;
		public float NightLightIntensity;
		public TimeMode(float _Spd, float _iDay, float _iNig)
		{
			TimeSpeed = _Spd;
			DayLightIntensity = _iDay;
			NightLightIntensity = _iNig;
		}

		public static bool operator ==(TimeMode _this, TimeMode _other)
		{
			return _this.DayLightIntensity == _other.DayLightIntensity && _this.NightLightIntensity == _other.NightLightIntensity && _this.TimeSpeed == _other.TimeSpeed;
		}
		public static bool operator !=(TimeMode _this, TimeMode _other)
		{
			return !(_this.DayLightIntensity == _other.DayLightIntensity && _this.NightLightIntensity == _other.NightLightIntensity && _this.TimeSpeed == _other.TimeSpeed);
		}
	}
	void ChangeMode(TimeMode Mode)
	{
		TimeChangeSpeed = Mode.TimeSpeed;
		StartCoroutine(IntensityChange(LightSourceDay.GetComponent<Light>(), LightSourceDay.GetComponent<Light>().intensity, Mode.DayLightIntensity, DayNightSwapSmooth));
		StartCoroutine(IntensityChange(LightSourceNight.GetComponent<Light>(), LightSourceNight.GetComponent<Light>().intensity, Mode.NightLightIntensity, DayNightSwapSmooth));
		_current = Mode;
	}
	// Update is called once per frame
	IEnumerator IntensityChange(Light light, float src, float des, float rate)
	{
		while (Mathf.Abs(light.intensity - des) > 0.1f)
		{
			light.intensity = Mathf.Lerp(light.intensity, des, rate * Time.deltaTime);
			yield return new WaitForEndOfFrame();
		}
		if (light.intensity < 0.1f)
		{
			light.intensity = 0;
		}
		else
		{
			light.intensity = des;
		}

	}
	void Update()
	{
		LightSourceDay.transform.Rotate(Vector3.right.normalized, TimeChangeSpeed * Time.deltaTime);
		LightSourceNight.transform.Rotate(-Vector3.right.normalized, TimeChangeSpeed * Time.deltaTime);
		//Debug.Log(LightSourceDay.transform.rotation.eulerAngles.x);
		if (LightSourceDay.transform.rotation.eulerAngles.x >= NightComeAngle && _current == Day)//Night Comes
		{
			ChangeMode(Night);
		}
		else if (LightSourceDay.transform.rotation.eulerAngles.x <= DayComeAngle && _current == Night)
		{
			ChangeMode(Day);
		}
	}
}
